var util = require('util');

module.exports = {

    format: function (tpl, req, res) {
        var me = this;
        return tpl.replace(/\$\{([^\{\}]*)\}/g, function (_, name) {
            var method = 'get' + name.charAt(0).toUpperCase() + name.substr(1);
            var value = me[method] && me[method](req, res);
            return value == null ? '' : value + '';
        });
    },
    /**
     * get request ip
     *
     * @param  {IncomingMessage} req
     * @return {string}
     */
    getIp: function (req) {
        return req.ip
        || req._remoteAddress
        || (req.connection && req.connection.remoteAddress)
        || undefined;
    },

    getUserAgent: function (req) {
        return req.headers['user-agent'];
    },


    getRequestHeader: function (req, res, field) {
        var header = req.headers[field.toLowerCase()];
        return Array.isArray(header) ? header.join(', ') : header;
    },

    getHttpVersion: function (req) {
        return req.httpVersionMajor + '.' + req.httpVersionMinor;
    },

    getDate: function (req, res, format) {
        var date = new Date()

      switch (format || 'web') {
          case 'clf':
          return clfdate(date)
          case 'iso':
          return date.toISOString()
          case 'web':
          return date.toUTCString()
      }
    },

    getRemoteUser: function (req) {

    },

    getReqContentLength: function () {

    },

    getMethod: function (req, res) {
        return req.method;
    },

    getUrl: function (req, res) {
        return req.url;
    },

    getStatus: function (req, res) {
        return res._header
        ? String(res.statusCode)
        : undefined
    },

    getReferrer: function (req) {
        return req.referrer;
    }

};
